//
//  JMRotationView.m
//  VoluntaryStar
//
//  Created by ios on 15/8/23.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMRotationView.h"
#import "JMRotationItem.h"

#define  RADIUS  UI_SCREEN_WITH *0.3125


@interface JMRotationView (){
    NSMutableArray *buttonArray;
    NSMutableArray *itemModeArray;
    CGPoint centPoint;//相对旋转的中心点
    NSString * endRotaionDegree;
}

@property (nonatomic,strong) UIView *container;

@property (nonatomic,strong) UIButton *leftBtn;

@property (nonatomic,strong) UIButton *centBtn;

@property (nonatomic,strong) UIButton *rightbtn;

@property (nonatomic,strong) UILabel *namelabel;
@end

@implementation JMRotationView

#pragma mark  初始化 固定控件
-(UIView *)container{
    if(!_container){
        _container = [[UIView alloc]init];
        _container.backgroundColor = [UIColor clearColor];
        _container.userInteractionEnabled = YES;
        if(iphone4){
            _container.frame = CGRectMake(0, -5, self.bounds.size.width, self.bounds.size.width);

        }else{
            _container.frame = CGRectMake(0, (UI_SCREEN_WITH - 320) * -0.2605 , self.bounds.size.width, self.bounds.size.width);
        }
    }
    return _container;
}

-(UIButton *)leftBtn{
    if(!_leftBtn){
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftBtn setImage:[UIImage imageNamed:@"ship.bundle/left_right"] forState:UIControlStateNormal];
        _leftBtn.tag = RotationViewButtonLeft;
        _leftBtn.userInteractionEnabled = NO;
//        [_leftBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        if(iphone4){
            _leftBtn.frame = CGRectMake(UI_SCREEN_WITH *0.08075, 0, UI_SCREEN_WITH *0.25 * 0.85, UI_SCREEN_WITH *0.25 * 0.85);
        }else{
            _leftBtn.frame = CGRectMake(UI_SCREEN_WITH *0.06575, 0, UI_SCREEN_WITH *0.25, UI_SCREEN_WITH *0.25);
        }
    }
    return _leftBtn;
}
-(UIButton *)centBtn{
    if(!_centBtn){
        _centBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_centBtn setImage:[UIImage imageNamed:@"ship.bundle/Outlook_image_center"] forState:UIControlStateNormal];
        _centBtn.tag = RotationViewButtonCenter;
        [_centBtn addTarget:self action:@selector(buttonClickCenter:) forControlEvents:UIControlEventTouchUpInside];
        if(iphone4){
            _centBtn.frame = CGRectMake(self.frameWidth/2.0 - UI_SCREEN_WITH *0.375 *0.85/2.0, 32, UI_SCREEN_WITH *0.375 *0.85, UI_SCREEN_WITH *0.375 * 0.85);
        }else{
            _centBtn.frame = CGRectMake(self.frameWidth/2.0 - UI_SCREEN_WITH *0.425/2.0, 20, UI_SCREEN_WITH *0.425, UI_SCREEN_WITH *0.425);
        }
    }
    return _centBtn;
}

-(UIButton *)rightbtn{
    if(!_rightbtn){
        _rightbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightbtn setImage:[UIImage imageNamed:@"ship.bundle/left_right"] forState:UIControlStateNormal];
        _rightbtn.tag = RotationViewButtonRight;
        _rightbtn.userInteractionEnabled = NO;
//        [_rightbtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        if(iphone4){
            _rightbtn.frame = CGRectMake(self.frameWidth - UI_SCREEN_WITH *0.09975 -UI_SCREEN_WITH *0.25 * 0.75, 0, UI_SCREEN_WITH *0.25 * 0.85, UI_SCREEN_WITH *0.25 * 0.85);
        }else{
            _rightbtn.frame = CGRectMake(self.frameWidth - UI_SCREEN_WITH *0.0625- UI_SCREEN_WITH *0.25, 0, UI_SCREEN_WITH *0.25, UI_SCREEN_WITH *0.25);
        }
    }
    return _rightbtn;
}

-(UILabel *)namelabel{
    if(!_namelabel){
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.centBtn.frameLeft + 15, self.centBtn.frameBottom + 2 , self.centBtn.frameWidth - 30, UI_SCREEN_WITH * 0.0625)];
        imageView.image = [UIImage imageNamed:@"ship.bundle/botton"];
        [self addSubview:imageView];
        _namelabel = [[UILabel alloc]init];
        _namelabel.backgroundColor = [UIColor clearColor];
        _namelabel.text = @"性格分析";
        _namelabel.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.05];
        _namelabel.textAlignment = NSTextAlignmentCenter;
        _namelabel.textColor = [UIColor whiteColor];
        _namelabel.frame = CGRectMake(0, 0 , imageView.frameWidth, imageView.frameHeight);
        [imageView addSubview:_namelabel];
    }
    return _namelabel;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        buttonArray = [[NSMutableArray alloc]initWithCapacity:0];
        itemModeArray = [[NSMutableArray alloc]initWithCapacity:0];
        endRotaionDegree = @"0";
        [self addSubview:self.container];
        [self addSubview:self.leftBtn];
        [self addSubview:self.centBtn];
        [self addSubview:self.rightbtn];
        self.namelabel.backgroundColor =[UIColor clearColor];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(turnContainer:) name:@"WHELLROTATION" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(turnContainerEnd:) name:@"WHELLROTATIONEND" object:nil];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
}
#pragma mark  设置
-(void)setRotationItem:(NSArray *)array{
    CGFloat centery = 35 + UI_SCREEN_WITH *0.425/2.0;
    CGFloat centerx = self.container.center.x;
    centPoint = CGPointMake(centerx, centery - 120);
    [itemModeArray addObjectsFromArray:array];
    for(int i = 0;i <array.count;i++){
        CGFloat tmpy =  centery + RADIUS *cos(2.0*M_PI *i/array.count);
        CGFloat tmpx =	centerx - RADIUS *sin(2.0*M_PI *i/array.count);
        JMRotationItem * item = [array objectAtIndex:i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(itemButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.userInteractionEnabled = NO;
        if(iphone4){
            btn.frame = CGRectMake(0, 0,UI_SCREEN_WITH *0.140025 *0.75,UI_SCREEN_WITH *0.140025 *0.75);
        }else{
            btn.frame = CGRectMake(0, 0,UI_SCREEN_WITH *0.140025,UI_SCREEN_WITH *0.140025);
        }
        btn.center = CGPointMake(tmpx, tmpy -115);
        btn.tag = i;
        [btn setImage:item.image forState:UIControlStateNormal];
        [self.container addSubview:btn];
        [buttonArray addObject:btn];
        [self updataWithSeleteItem];
        [self hideButton:btn];
    }
    [self setNeedsDisplay];
    [self setNeedsLayout];
}
-(void)itemButtonClick:(UIButton *)sender{
}
-(void)buttonClickCenter:(UIButton *)sender{
    if([self.deleagte respondsToSelector:@selector(rotationViewButtonClick:)]){
        RotationViewItemType type;
        if([endRotaionDegree isEqualToString:@"0"]){
            type = RotationViewItemPuTongKaoSheng;
        }else if ([endRotaionDegree isEqualToString:@"60"]){
            type = RotationViewItemXingGeCeShi;
        }else if ([endRotaionDegree isEqualToString:@"120"]){
            type = RotationViewItemXiaLingYing;
        }else if ([endRotaionDegree isEqualToString:@"180"]){
            type = RotationViewItemWoShiXueBa;
        }else if ([endRotaionDegree isEqualToString:@"-120"]){
            type = RotationViewItemYiTiKaoSheng;
        }else if ([endRotaionDegree isEqualToString:@"-60"]){
            type = RotationViewItemZiZhuZhaoSheng;
        }
        [self.deleagte rotationViewButtonClick:type];
    }else{
        @throw [NSException exceptionWithName:NSExpansionAttributeName reason:@"没有实现代理方法" userInfo:nil];
    }
}

#pragma mark 拖动旋转当中
-(void)turnContainer:(NSNotification *)notion{
    NSString * str = [notion.object objectForKey:@"angDifferent"];
    CGFloat angDifferent = [str floatValue];
    [self rotationalDegree:angDifferent];
}
#pragma mark  拖动旋转结束
-(void)turnContainerEnd:(NSNotification *)notion{
    NSString * str = [notion.object objectForKey:@"angDifferent"];
    int angDifferent = [str floatValue];
    DDLOG(@"DDDE::%d",angDifferent );///多少度
    endRotaionDegree = [NSString stringWithFormat:@"%d",angDifferent];
    [self rotationalDegree:angDifferent];
    DDLOG(@"GHGH::%@",endRotaionDegree);
    [self updataWithSeleteItem];
}
#pragma mark  重新定义按钮坐标
-(void)rotationalDegree:(float)dushu{
    CGFloat centery = 35 + UI_SCREEN_WITH *0.425/2.0;
    CGFloat centerx = self.container.center.x;
    for(int i = 0;i <buttonArray.count;i++){
        CGFloat tmpy =  centery + RADIUS *cos(2.0*M_PI *i/buttonArray.count + (dushu/360 *2*M_PI));
        CGFloat tmpx =	centerx - RADIUS *sin(2.0*M_PI *i/buttonArray.count + (dushu/360 *2*M_PI));
        UIButton *btn = [buttonArray objectAtIndex:i];
        btn.center = CGPointMake(tmpx, tmpy -115);
        [self hideButton:btn];
    }
    [self setNeedsLayout];
}
#pragma mark  超出界面的按钮隐藏
-(void)hideButton:(UIButton *)button{
    CGRect rect = [button convertRect:button.bounds toView:self.container];
    if (CGRectIntersectsRect(self.container.frame, rect)) {
        button.hidden = NO;
    }else{
         button.hidden = YES;
    }
}
#pragma mark  选中某个按钮更新 相关信息
-(void)updataWithSeleteItem{
    NSString *title;
    if([endRotaionDegree isEqualToString:@"0"]){
        title = [[itemModeArray objectAtIndex:0] title];
    }else if ([endRotaionDegree isEqualToString:@"60"]){
        title = [[itemModeArray objectAtIndex:5] title];
    }else if ([endRotaionDegree isEqualToString:@"120"]){
        title = [[itemModeArray objectAtIndex:4] title];
    }else if ([endRotaionDegree isEqualToString:@"180"]){
        title = [[itemModeArray objectAtIndex:3] title];
    }else if ([endRotaionDegree isEqualToString:@"-120"]){
        title = [[itemModeArray objectAtIndex:2] title];
    }else if ([endRotaionDegree isEqualToString:@"-60"]){
        title = [[itemModeArray objectAtIndex:1] title];
    }
    self.namelabel.text =title;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end



















